package hieuph.facerecognition.FaceDetect;

import hieuph.facerecognition.ManipulationPicture.MyImageView;
import hieuph.facerecognition_datn.R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.opencv.android.Utils;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

import android.content.Context;
import android.util.Log;

public class FaceDetect {
	private static final String TAG = "FaceDetect";

	// Do openCv dinh nghia CV_RGB2GRAY = 7
	public static final int CV_RGB2GRAY = 7;

	private static final Scalar FACE_RECT_COLOR = new Scalar(0, 255, 0, 255);
	private float mRelativeFaceSize = 0.2f;
	private int mAbsoluteFaceSize = 0;

	public static CascadeClassifier getJavaDetector(Context mContext)
			throws IOException {

		// Bien lien quan nhan dang lop
		File mCascadeFile;
		CascadeClassifier mJavaDetector;

		// load cascade file from application resources
		InputStream is = mContext.getResources().openRawResource(
				R.raw.lbpcascade_frontalface);
		File cascadeDir = mContext.getDir("cascade", Context.MODE_PRIVATE);
		mCascadeFile = new File(cascadeDir, "lbpcascade_frontalface.xml");
		FileOutputStream os = new FileOutputStream(mCascadeFile);

		byte[] buffer = new byte[4096];
		int bytesRead;
		while ((bytesRead = is.read(buffer)) != -1) {
			os.write(buffer, 0, bytesRead);
		}
		is.close();
		os.close();

		mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath());
		if (mJavaDetector.empty()) {
			Log.e(TAG, "Failed to load cascade classifier");
			mJavaDetector = null;
		} else
			Log.i(TAG,
					"Loaded cascade classifier from "
							+ mCascadeFile.getAbsolutePath());

		cascadeDir.delete();
		return mJavaDetector;

	}

	public Rect[] detectFace(MyImageView img, CascadeClassifier mJavaDetector) {
		Mat grayImg = new Mat();
		Mat orginalImg = new Mat();
		Utils.bitmapToMat(img.getMyBitmap(), orginalImg);
		Imgproc.cvtColor(orginalImg, grayImg, CV_RGB2GRAY);
		if (mAbsoluteFaceSize == 0) {
			int height = grayImg.rows();
			if (Math.round(height * mRelativeFaceSize) > 0) {
				this.mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
			}
		}
		MatOfRect faces = new MatOfRect();
		if (mJavaDetector != null)
//			mJavaDetector.detectMultiScale(grayImg, faces, 1.1, 2, 2, // TODO:
//																		// objdetect.CV_HAAR_SCALE_IMAGE
			mJavaDetector.detectMultiScale(grayImg, faces, 1.1, 3, 2, // TODO:
					// objdetect.CV_HAAR_SCALE_IMAGE
					
					new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());
		Rect[] facesArray = faces.toArray();
		grayImg = null;
		orginalImg = null;
		return facesArray;

	}

	public void setMinFaceSize(float faceSize) {
		faceSize = (float) faceSize / 100;
		mRelativeFaceSize = faceSize;
		mAbsoluteFaceSize = 0;
	}

	public float getmRelativeFaceSize() {
		return mRelativeFaceSize;
	}

	public void setmRelativeFaceSize(float mRelativeFaceSize) {
		this.mRelativeFaceSize = mRelativeFaceSize;
	}

	public int getmAbsoluteFaceSize() {
		return mAbsoluteFaceSize;
	}

	public void setmAbsoluteFaceSize(int mAbsoluteFaceSize) {
		this.mAbsoluteFaceSize = mAbsoluteFaceSize;
	}

	public native static void convertRGBToGray(int width, int height,
			int[] mPhotoIntArray);
}
